package hczc.longcai.com.hccrowdfunding.home.fourth_mine.activities;

import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import hczc.longcai.com.hccrowdfunding.R;
import hczc.longcai.com.hccrowdfunding.activity.BaseActivity;
import hczc.longcai.com.hccrowdfunding.home.fourth_mine.FourthFragment;
import hczc.longcai.com.hccrowdfunding.home.fourth_mine.fragments.RewardOrderAllFragment;
import hczc.longcai.com.hccrowdfunding.home.fourth_mine.fragments.RewardOrderDfhFragment;
import hczc.longcai.com.hccrowdfunding.home.fourth_mine.fragments.RewardOrderDshFragment;
import hczc.longcai.com.hccrowdfunding.home.fourth_mine.fragments.RewardOrderDzfFragment;

public class RewardOrdersActivity extends BaseActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {

    private RelativeLayout all;
    private TextView all_text;
    private View all_line;

    private RelativeLayout dzf;
    private TextView dzf_text;
    private View dzf_line;

    private RelativeLayout dfh;
    private TextView dfh_text;
    private View dfh_line;

    private RelativeLayout dsh;
    private TextView dsh_text;
    private View dsh_line;

    private ViewPager viewPager;
    private OrderPagerFragmentAdapter pagerFragmentAdapter;

    private List<TextView> textViews;
    private List<View> views;

    private List<Fragment> fragments;

    private RewardOrderAllFragment allFragment;
    private RewardOrderDfhFragment dfhFragment;
    private RewardOrderDshFragment dshFragment;
    private RewardOrderDzfFragment dzfFragment;

    private int state;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reward_orders);

        initDatas();
        initFragments();
        initViews();
    }

    @Override
    protected void onStart() {
        super.onStart();
        changeViewPager();

    }

    private void changeViewPager() {
        switch (state){
            case FourthFragment.STATE_ALL:
                viewPager.setCurrentItem(0);
                break;
            case FourthFragment.STATE_DZF:
                viewPager.setCurrentItem(1);
                break;
            case FourthFragment.STATE_DFH:
                viewPager.setCurrentItem(2);
                break;
            case FourthFragment.STATE_DSH:
                viewPager.setCurrentItem(3);
                break;
        }
    }

    private void initDatas() {
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        state = extras.getInt("state");
    }

    private void initFragments() {
        fragments = new ArrayList<>();

        allFragment = new RewardOrderAllFragment();
        fragments.add(allFragment);

        dzfFragment = new RewardOrderDzfFragment();
        fragments.add(dzfFragment);

        dfhFragment = new RewardOrderDfhFragment();
        fragments.add(dfhFragment);

        dshFragment = new RewardOrderDshFragment();
        fragments.add(dshFragment);


    }

    private void initViews() {
        textViews = new ArrayList<>();
        views = new ArrayList<>();

        findViewById(R.id.head_back).setOnClickListener(this);
        ((TextView) findViewById(R.id.head_title)).setText("我的订单");

        all = ((RelativeLayout) findViewById(R.id.mine_reward_orders_tabs_all));
        all.setOnClickListener(this);
        all_text = ((TextView) findViewById(R.id.mine_reward_orders_tabs_all_text));
        textViews.add(all_text);
        all_line = findViewById(R.id.mine_reward_orders_tabs_all_line);
        all_line.setSelected(true);
        views.add(all_line);

        dzf = ((RelativeLayout) findViewById(R.id.mine_reward_orders_tabs_dzf));
        dzf.setOnClickListener(this);
        dzf_text = ((TextView) findViewById(R.id.mine_reward_orders_tabs_dzf_text));
        dzf_text.setTextColor(Color.parseColor("#8e8e8e"));
        textViews.add(dzf_text);
        dzf_line = findViewById(R.id.mine_reward_orders_tabs_dzf_line);
        views.add(dzf_line);

        dfh = ((RelativeLayout) findViewById(R.id.mine_reward_orders_tabs_dfh));
        dfh.setOnClickListener(this);
        dfh_text = ((TextView) findViewById(R.id.mine_reward_orders_tabs_dfh_text));
        dfh_text.setTextColor(Color.parseColor("#8e8e8e"));
        textViews.add(dfh_text);
        dfh_line = findViewById(R.id.mine_reward_orders_tabs_dfh_line);
        views.add(dfh_line);

        dsh = ((RelativeLayout) findViewById(R.id.mine_reward_orders_tabs_dsh));
        dsh.setOnClickListener(this);
        dsh_text = ((TextView) findViewById(R.id.mine_reward_orders_tabs_dsh_text));
        dsh_text.setTextColor(Color.parseColor("#8e8e8e"));
        textViews.add(dsh_text);
        dsh_line = findViewById(R.id.mine_reward_orders_tabs_dsh_lind);
        views.add(dsh_line);

        viewPager = ((ViewPager) findViewById(R.id.mine_reward_orders_viewpager));
        viewPager.setOnPageChangeListener(this);
        pagerFragmentAdapter = new OrderPagerFragmentAdapter(getSupportFragmentManager());
        viewPager.setAdapter(pagerFragmentAdapter);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.head_back:
                finish();
                break;
            case R.id.mine_reward_orders_tabs_all:
                viewPager.setCurrentItem(0);
                break;
            case R.id.mine_reward_orders_tabs_dzf:
                viewPager.setCurrentItem(1);
                break;
            case R.id.mine_reward_orders_tabs_dfh:
                viewPager.setCurrentItem(2);
                break;
            case R.id.mine_reward_orders_tabs_dsh:
                viewPager.setCurrentItem(3);
                break;
        }
    }

    class OrderPagerFragmentAdapter extends FragmentPagerAdapter{

        public OrderPagerFragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

        for(int i = 0;i < textViews.size();i++){
            textViews.get(i).setTextColor(Color.parseColor("#8e8e8e"));
            views.get(i).setSelected(false);
        }
        textViews.get(position).setTextColor(Color.parseColor("#303030"));
        views.get(position).setSelected(true);

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
